﻿using System.Data.Services;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NuGet.Server.DataServices;

namespace MayhemServer {
    public class MvcApplication : HttpApplication {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Home",
                "",
                new { controller = "Home", action = "Index" }
            );

            // The default route is /nuget/Packages
            var serviceRoute = new ServiceRoute("nuget", new DataServiceHostFactory(), typeof(Packages));
            serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" } };
            serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };
            routes.Add("nuget", serviceRoute);

            routes.MapRoute(
                "Packages",
                "Packages/All",
                new { controller = "Package", action = "List" }
            );

            routes.MapRoute(
                "Package",
                "Packages/{id}",
                new { controller = "Package", action = "Details" }
            );

            //routes.IgnoreRoute("Packages/{id}.nupkg");

            routes.MapRoute(
                "Download",
                "Packages/Download/{id}",
                new { controller = "Package", action = "Download" }
            );

			routes.MapRoute(
				"Success",
				"Contest/Sucess",
				new { controller = "Contest", action = "Success" }
			);

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}